<template>
	<view class="snowy-page">
		<view class="snowy-flex-vh-center logo-content">
			<image class="logo" :alt="sysBaseConfig.SNOWY_SYS_NAME" :src="sysBaseConfig.SNOWY_SYS_LOGO" mode="scaleToFill">
			</image>
			<text class="title">{{sysBaseConfig.SNOWY_SYS_NAME}} {{sysBaseConfig.SNOWY_SYS_VERSION}}</text>
		</view>
		<view class="login-form">
			<view class="snowy-shadow snowy-flex-v-center input-item">
				<uni-icons class="icon" type="person-filled" size="20" color="#5677fc"></uni-icons>
				<input v-model="loginForm.account" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="snowy-shadow snowy-flex-v-center input-item">
				<uni-icons class="icon" type="locked-filled" size="20" color="#5677fc"></uni-icons>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="snowy-shadow snowy-flex-v-center input-item" v-if="sysBaseConfig.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN === 'true'">
				<uni-icons class="icon" type="map-filled" size="20" color="#5677fc"></uni-icons>
				<input v-model="loginForm.validCode" type="text" class="input" placeholder="请输入验证码" maxlength="4" />
				<image :src="validCodeBase64" @click="loginCaptcha" class="login-code-img" mode="scaleToFill"></image>
			</view>
			<view>
				<tui-button margin="70rpx 0" :preventClick="true" :shadow="true" @click="handleLogin">登录</tui-button>
			</view>
		</view>
	</view>
</template>
<script setup>
	import { ref, reactive, getCurrentInstance, computed } from 'vue'
	import loginApi from '@/api/auth/login-api'
	import store from '@/store'
	import config from '@/config'
	const { proxy } = getCurrentInstance()
	// 验证码
	const validCodeBase64 = ref("")
	// 登录表单
	const loginForm = reactive({
		account: 'admin',
		password: '123456',
		validCode: '',
	})
	//store.dispatch('GetSysBaseConfig')
	// 系统配置
	const sysBaseConfig = computed(() => {
		return store.getters.sysBaseConfig
	})
	// 当前环境
	const curEnv = computed(() => {
		return store.getters.curEnv
	})
	
	// 获取图形验证码
	const loginCaptcha = async () => {
		const data = await loginApi.getPicCaptcha()
		validCodeBase64.value = data
	}
	if (sysBaseConfig.value.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN) {
		loginCaptcha()
	}
	// 登录方法
	const handleLogin = () => {
		if (loginForm.account === "") {
			uni.$snowy.modal.msgError("请输入账号")
		} else if (loginForm.password === "") {
			uni.$snowy.modal.msgError("请输入密码")
		} else if (loginForm.validCode === "" && sysBaseConfig.value.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN === 'true') {
			uni.$snowy.modal.msgError("请输入验证码")
		} else {
			pwdLogin()
		}
	}
	// 密码登录
	const pwdLogin = () => {
		store.dispatch('Login', loginForm).then((data) => {

			// 所有异步请求结束之后才能够进行下一步操作
			Promise.all([
				store.dispatch('GetUserLoginMenu'),
				store.dispatch('GetUserInfo',data),
			]).then((result) => {
				// #ifdef H5
				const { searchQuery, hashQuery } = uni.$xeu.parseUrl(location.href)
				if (searchQuery.redirect) {
					uni.$snowy.tab.reLaunch(searchQuery.redirect)
				} else if (hashQuery.redirect) {
					uni.$snowy.tab.reLaunch(hashQuery.redirect)
				} else {
					// #endif
					uni.$snowy.tab.reLaunch('/pages/home/index')
					// #ifdef H5
				}
				// #endif
			}).catch(err => {
				if (sysBaseConfig.value.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN) {
					loginCaptcha()
					loginForm.validCode = ""
				}
			})
		}).catch(err => {
			if (sysBaseConfig.value.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN) {
				loginCaptcha()
				loginForm.validCode = ""
			}
		})
	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}

	.logo-content {
		margin: 15% auto;

		.logo {
			width: 100rpx;
			height: 100rpx;
		}

		.title {
			font-size: 40rpx;
			margin-left: 15rpx;
		}
	}

	.login-form {
		margin: 60rpx auto;
		width: 80%;

		.input-item {
			margin: 35rpx auto;
			background-color: #f5f6f7;
			height: 90rpx;

			.icon {
				margin-left: 20rpx;
			}

			.input {
				width: 100%;
				font-size: 30rpx;
				padding-left: 25rpx;
			}

			.login-code-img {
				cursor: pointer;
				width: 70%;
				height: 85rpx
			}
		}
	}
</style>